package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import com.itheima.tlias84.po.InsertDeptParam;
import com.itheima.tlias84.service.DeptService;
import com.itheima.tlias84.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
/**
 * @Resource 优先属性注入的使用
 *           默认按照Bean名字从容器中获取
 *           默认的SpringBean名字是类对应的小驼峰
 *           不同包下有可能出现同名类
 * @Compoment注解在创建Bean的时候指定名字
 * @Resource从ioc容器中获取时也指定名字
 *
 */
public class DeptController {
//    属性注入优先使用Resource
    @Autowired
    DeptService deptService;
    /**
     * get/delete 请求参数一般拼接到url后面
     * post/put 请求参数一般放在请求体里面，且JSON格式进行传输
     * @return
     */
    @GetMapping
    public ResultUtil selectAll() {
        List<SelectAllDeptDTO> selectAllDeptDTOS = deptService.selectAll();
        return ResultUtil.success(selectAllDeptDTOS);
    }
    @PostMapping
    public ResultUtil insertDept(@RequestBody InsertDeptParam param){
        int flag = deptService.insertDept(param);
        return flag==0?ResultUtil.fail("新增失败"):ResultUtil.success("新增成功");
    }
    @DeleteMapping
    public ResultUtil deleteById(Integer id) {
        int i = deptService.deleteById(id);
        return i==0?ResultUtil.fail():ResultUtil.success();
    }
    @GetMapping("/{id}")
    public ResultUtil  selectDeptById(@PathVariable Integer id){
        log.info("使用PathVariable获得的注解是={}",id);
        SelectAllDeptDTO dto = deptService.selectDeptById(id);
        return ResultUtil.success(dto);
    }
    @PutMapping
    public ResultUtil updateDeptById(@RequestBody Dept dept){
        log.info("名字{}",dept.getName());
        int i = deptService.updateDeptById(dept);
        return i==0?ResultUtil.fail():ResultUtil.success();
    }
//    @PostMapping
//    public ResultUtil getName(@RequestBody InsertDeptParam  param) {
//        //把json字符串转化成对象
////        log.info("读取请求体中的数据{}"+param.getName());
////        try {
////            BufferedReader reader = request.getReader();
////            log.info("读取请求体中的数据{}"+reader.readLine());
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//        Dept dept = new Dept();
//        dept.setName(param.getName());
//        dept.setUpdateTime(LocalDateTime.now());
//        dept.setCreateTime(LocalDateTime.now());
//        int flag = deptMapper.insertDept(dept);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code",flag);
//        map.put("msg","查询成功");
//        map.put("data",null);
//        return ResultUtil.success(flag);
//    }

//


}
